package com.kinon.norogamilk.http;

import com.kinon.norogamilk.bean.BaseBean;
import com.kinon.norogamilk.bean.SpecBean;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import io.reactivex.Flowable;
import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

/**
 * Created by lhqq on 2017-12-19.
 */

public class MilkApi {

    private static final int TIME_OUT = 10; //超时参数
    private static MilkApi milkApi = null;
    private static ApiService apiService = null;
    private String url = "http://test.jishi88.cn/autob/index.php/";

    public MilkApi() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                //增加返回值为string的支持
                .addConverterFactory(ScalarsConverterFactory.create())
                // 添加Rx适配器
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                // 添加Gson转换器
                .addConverterFactory(GsonConverterFactory.create())
                .client(getHttpClient())
                .build();

        apiService = retrofit.create(ApiService.class);
    }

    public static MilkApi getInstance() {
        if (milkApi == null) {
            milkApi = new MilkApi();
        }
        return milkApi;
    }

    private OkHttpClient getHttpClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .connectTimeout(TIME_OUT, TimeUnit.SECONDS)
                .readTimeout(TIME_OUT, TimeUnit.SECONDS)
                .writeTimeout(TIME_OUT, TimeUnit.SECONDS)
                // 失败重发
                .retryOnConnectionFailure(true)
                .addInterceptor(new LogInterceptor());
        return builder.build();
    }

    public Call<String> getAd(String code) {
        return apiService.getAd(code);
    }

    public Flowable<BaseBean<ArrayList<SpecBean>>> getGoods(String code) {
        return apiService.getGoods(code);
    }

    public Call<String> memberCheck(String code, String phone) {
        return apiService.memberCheck(code, phone);
    }

    public Call<String> saveOrder(String code, String productid,
                                  String amount, String userid) {
        return apiService.saveOrder(code, productid, amount, userid);
    }

    public Flowable<BaseBean> restock(String code, String data) {
        return apiService.restock(code, data);
    }

    public Call<String> machinePw(String code, String goods) {
        return apiService.machinePw2(code, goods);
    }

    public Call<String> getProductByCode(String code) {
        return apiService.getProductByCode(code);
    }

    public Call<String> getProductByPickup(String code, String pwd) {
        return apiService.getProductByPickup(code, pwd);
    }

    public Call<String> updateOrder(String code, String orderid) {
        return apiService.updateOrder(code, orderid);
    }

    public Call<String> getURL(String code) {
        return apiService.getURL(code);
    }

    public Call<String> getRaodInfo(String code) {
        return apiService.getRaodInfo(code);
    }

    public Call<String> machineWs(String code) {
        return apiService.machineWs(code);
    }
}
